/* eslint-disable no-unused-vars */
import React, { useState } from 'react';
import { Button, Input } from 'antd';
import { useRequest } from 'ahooks';
import RequestApi from '@/utils/request';

function EditorTip(props) {
	const { form } = props;
	const info = [];
	async function change(e) {
		// 排出特殊情况防止请求出错
		if (e !== '' && e !== undefined && e !== null && e !== ' ') {
			form.setFieldsValue({
				name: e,
			});
			RequestApi.connection('get', `parkLot/getListByName/${e}`).then((response) => {
				const list = response.data.rows;
				for (let i = 0; i < list.length; i += 1) {
					info.push(list[i].name);
				}
			});
		}
		return new Promise((resolve) => {
			setTimeout(() => {
				resolve(info);
			}, 300);
		});
	}
	const { data, loading, run } = useRequest(change, {
		debounceInterval: 500,
		manual: true,
	});
	return (
		<>
			<Input placeholder="请输入" onChange={(e) => run(e.target.value)} width={200} />
			{loading ? null : (
				<div
					style={{
						marginTop: 5,
						boxShadow: '1px 1px 5px #888888',
						position: 'absolute',
						width: '247px',
						backgroundColor: 'white',
						zIndex: '100',
					}}
				>
					{data?.map((i) => (
						<div key={i} style={{ color: 'black' }}>
							<Button style={{ color: 'black' }} type="link">
								{i}
							</Button>
						</div>
					))}
				</div>
			)}
		</>
	);
}

export default EditorTip;
